//    Copyright (c) 2009 Elwyn Malethan
//
//    This file is part of SeemoreJ.
//
//    SeemoreJ is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    SeemoreJ is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with SeemoreJ.  If not, see <http://www.gnu.org/licenses/>.

package com.malethan.seemorej.hibernate;

import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;

public class TimestampInterceptor extends EmptyInterceptor {
    //----------------------------------------------------------------------- Static Properties and Constants
    //----------------------------------------------------------------------- Static Methods
    //----------------------------------------------------------------------- Instance Properties
    //----------------------------------------------------------------------- Constructors
    //----------------------------------------------------------------------- Getters and Setters
    //----------------------------------------------------------------------- Instance Methods

    /**
     * Called when an object is detected to be dirty, during a flush. The interceptor may modify the
     * detected currentState, which will be propagated to both the database and the persistent object.
     * Note that not all flushes end in actual synchronization with the database, in which case the
     * new currentState will be propagated to the object, but not necessarily (immediately) to the
     * database. It is strongly recommended that the interceptor not modify the previousState.
     *
     * @return true if the user modified the currentState in any way. 
     */
    public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState,
                                Object[] previousState, String[] propertyNames,
                                Type[] types) {
        setValue(currentState, propertyNames, "updatedOn", new Date());
        return true;
    }

    

    /**
     * Called before an object is saved. The interceptor may modify the state, which will be used for
     * the SQL INSERT and propagated to the persistent object.
     * 
     * @return true if the user modified the state in any way.
     */
    public boolean onSave(Object entity, Serializable id, Object[] state,
                          String[] propertyNames, Type[] types) {
        setValue(state, propertyNames, "createdOn", new Date());
        setValue(state, propertyNames, "updatedOn", new Date());
        return true;
    }

    private void setValue(Object[] state, String[] propertyNames, String propertyToSet, Object value) {
        int index = Arrays.asList(propertyNames).indexOf(propertyToSet);
        if (index >= 0) {
            state[index] = value;
        }
    }
}
